package com.example.administrator.jingpingou.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.adapter.MineTaoCatAdapter;
import com.example.administrator.jingpingou.bean.CommodityBean;

import org.xutils.DbManager;
import org.xutils.common.util.KeyValue;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

public class MineTaoCatActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{
    private ListView mListView;
    private DbManager mDb;
    private List<CommodityBean> mData;
    private MineTaoCatAdapter mAdapter;
    private TextView mTextViewPrice;
    private CheckBox mCheckBox;
    private Button mButtonShop;
    private Integer position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_tao_cat);
        iniView();
        iniData();
        setData();
    }

    private void setData() {
        mListView.setAdapter(mAdapter);

    }

    private void iniData() {
        mData=new ArrayList<>();
        mAdapter=new MineTaoCatAdapter(mData,this);

        DbManager.DaoConfig config=new DbManager.DaoConfig();
        mDb= x.getDb(config);
        readDb();

        
    }

    private void readDb() {
       mData.clear();
        mAdapter=new MineTaoCatAdapter(mData,this);
        try {
            List<CommodityBean> all = mDb.selector(CommodityBean.class).findAll();
            if(all!=null) {
                mData.addAll(all);
                mAdapter.notifyDataSetChanged();

            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    private void iniView() {
        mListView= (ListView) findViewById(R.id.lv_minecat_test);
        mButtonShop= (Button) findViewById(R.id.bt_minecat_pay);
        mCheckBox= (CheckBox) findViewById(R.id.cb_minecat_checkall);
        mTextViewPrice= (TextView) findViewById(R.id.tv_minecat_count);
    }

    @Override
    public void onClick(View v) {
        if(v!=null) {
            int count = 0;
            position= (Integer) v.getTag();
            CommodityBean commodityBean = mData.get(position);
            try {
                List<CommodityBean> name = mDb.selector(CommodityBean.class).where("title", "=", commodityBean.getTitle()).findAll();
                String countS = name.get(0).getCount();
               count = Integer.parseInt(countS);

            } catch (DbException e) {
                e.printStackTrace();
            }
            KeyValue value=null;

            switch (v.getId()) {
                case  R.id.bt_minecat_pay:

                    break;
                case R.id.bt_itemcat_add:
                    if(position!=null) {
                        commodityBean.setCount(""+(count++));
                        Log.i("TAG", "===count"+count);
                        value=new KeyValue("count",count);
                        try {
                            mDb.update(CommodityBean.class, WhereBuilder.b("title","=",commodityBean.getTitle()),value);
                            readDb();
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                        Toast.makeText(MineTaoCatActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.bt_itemcat_delete:
                        if(position!=null) {
//                            mData.remove((int)position);

                            try {
                                mDb.delete(CommodityBean.class,WhereBuilder.b("title","=",commodityBean.getTitle()));
                                readDb();
                            } catch (DbException e) {
                                e.printStackTrace();
                            }

                        }
                    break;
                case R.id.bt_itemcat_sub:

                    if(position!=null) {
                        commodityBean.setCount(""+(count--));
                        Log.i("TAG", "===count"+count);
                        value=new KeyValue("count",count);
                        try {
                            mDb.update(CommodityBean.class, WhereBuilder.b("title","=",commodityBean.getTitle()),value);
                            readDb();
                        } catch (DbException e) {
                            e.printStackTrace();
                        }

                        Toast.makeText(MineTaoCatActivity.this, "更新成功", Toast.LENGTH_SHORT).show();

                    }

                    break;
            }
            mListView.setAdapter(mAdapter);

        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        
        switch (buttonView.getId()) {
            case  R.id.cb_minecat_checkall:
//                List<CommodityBean> all = null;
                KeyValue value=null;
                try {
//                    all = mDb.selector(CommodityBean.class).findAll();
                    for (int i = 0; i <mData.size() ; i++) {
                        value=new KeyValue("isChecked",isChecked);
                        CommodityBean commodityBean = mData.get(i);
//                        commodityBean.setChecked(isChecked);
                        mDb.update(CommodityBean.class, WhereBuilder.b("pic_url","=",commodityBean.getPic_url()),value);

                        readDb();
//                        mData.get(i).setChecked(isChecked);
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.cb_itemcat_check:
                
                  position= (Integer) buttonView.getTag();
                if(position!=null) {
                    CommodityBean commodityBean1 = mData.get(position);
                    KeyValue valuename=new KeyValue("isChecked",isChecked);
                    try {
                        mDb.update(CommodityBean.class,WhereBuilder.b("pic_url","=",commodityBean1.getPic_url()),valuename);
                        readDb();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }
                mAdapter.notifyDataSetChanged();
                break;
        }
        float money = mAdapter.getMoney();
        mTextViewPrice.setText(money+"");
//        mListView.setAdapter(mAdapter);
//        setData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition(0,R.anim.activity_finish);

    }
}
